package com.company;

/**
 * @author zhf
 * @date 2022/2/14
 */
public class CycleList_02 {
    public ListNode detectCycle(ListNode head) {
        if (head == null){
            return null;
        }
        ListNode ans = head;
        ListNode fast = head;
        ListNode low = head;
        while (fast != null && fast.next != null){
            //快指针移动两个，慢指针移动一个；
            fast = fast.next.next;
            low = low.next;
            if (fast == low) {
                while (ans != low) {
                    ans = ans.next;
                    low = low.next;
                }
                return ans;
            }
        }
        return null;
    }
}
